/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.zbxsoft.utr.service.plugins  
 */
package com.apache.bid.service.plugins;

import java.util.Date;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.BidCommonUtil;
import com.apache.bid.common.Constant;
import com.apache.bid.common.vo.BidItemCacheVo;
import com.apache.bid.entity.BidItem;
import com.apache.bid.manager.BidItemManager;
import com.apache.cache.util.Validator;
import com.apache.tools.DateUtils;

/**
 * description:  继续启用item，
 * @author xhe 创建时间：2016-03-21  
 * 
 */
public class UnityContinueItemPluginImpl implements PluginConnector {
	private Logger log = Logger.getLogger(getClass());
	protected BidItemManager bidItemManager;

	public Object execute(ParamsVo param) throws Exception {
		String itemId = Validator.getDefaultStr(param.getInfoId(), String.valueOf(param.getParams("infoId")));
		log.info("开始继续启用标的...");
		ResultEntity entity = new ResultEntity();
		if (Validator.isNull(itemId)) {
			entity.setResult("false");
			entity.setMessage("缺少方法参数，标的发布失败");
			return entity;
		}
		ParamsVo<BidItem> vo = new ParamsVo<BidItem>();
		vo.setInfoId(itemId);
		vo.setMethodKey("ByIdVo");
		BidItemCacheVo bidItemCacheVo = (BidItemCacheVo) bidItemManager.getInfoById(vo);
		if (Validator.isEmpty(bidItemCacheVo)) {
			entity.setResult("false");
			entity.setMessage("没有获取到标的信息，发布操作失败");
			return entity;
		}
		String status = bidItemCacheVo.getItemStatus();
		if (Constant.getInstance().getItemConfig("statusPubslish").equals(status)) {
			entity.setResult("false");
			entity.setMessage("此标的已经发布，不需要重复发布操作");
			return entity;
		}
		String firstBidTime = String.valueOf(param.getParams("firstBidTime"));
		firstBidTime = Validator.getDefaultStr(firstBidTime, DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		bidItemCacheVo.setFirstBidTimePre(firstBidTime);
		bidItemCacheVo.setCloseTimePre(setCloseTime(bidItemCacheVo.getBidMode(), bidItemCacheVo.getLimitTime(),
				firstBidTime));
		//更新缓存+数据库状态
		UnityItemPluginHelper.getInstance().resetItem(bidItemCacheVo, true);
		entity.setMessage("操作成功");
		entity.setEntity("true");
		entity.setResult("true");
		return entity;
	}

	private String setCloseTime(String bidMode, String limitTime, String firstBidTime) throws Exception {
		String colseTime = "";
		if (Constant.BIDMODE_JTZX.equals(bidMode) || Constant.BIDMODE_JTFX.equals(bidMode)) {
			colseTime = BidCommonUtil.getInstance().getMergeTime(firstBidTime, limitTime);
		} else if (Constant.BIDMODE_YCXASC.equals(bidMode) || Constant.BIDMODE_YCXDESC.equals(bidMode)) {
			colseTime = firstBidTime;
		} else if (Constant.BIDMODE_HELAN.equals(bidMode)) {
			long limittime = Long.valueOf(limitTime) * 1000 * 2;
			Date data = new Date(DateUtils.toLong(DateUtils.parse(firstBidTime, "yyyy-MM-dd HH:mm:ss")) + limittime);
			colseTime = DateUtils.format(data, "yyyy-MM-dd HH:mm:ss");
		} else if (Constant.BIDMODE_FEJJZX.equals(bidMode) || Constant.BIDMODE_FEJJFX.equals(bidMode)) {
			colseTime = BidCommonUtil.getInstance().getMergeTime(firstBidTime, limitTime);
		}
		return colseTime;
	}

	public void setBidItemManager(BidItemManager bidItemManager) {
		this.bidItemManager = bidItemManager;
	}

}
